package com.siants.common.pipeline;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.annotation.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

;


/**
 * Created by yanxin on 2016/9/14.
 */
@ThreadSafe
public class CommonPipeline extends FilePersistentBase implements Pipeline {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public CommonPipeline() {
        this.setPath("/data/webmagic/");
    }

    public CommonPipeline(String path) {
        this.setPath(path);
    }

    public void process(ResultItems resultItems, Task task) {
        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR;

        try {
            PrintWriter e = new PrintWriter(new OutputStreamWriter(new FileOutputStream(this.getFile(path + resultItems.getRequest().getUrl().split("http://shiro.apache.org/")[1] + ".html")), "UTF-8"));
            e.println("url:\t" + resultItems.getRequest().getUrl());
            Iterator i$ = resultItems.getAll().entrySet().iterator();

            while(true) {
                while(i$.hasNext()) {
                    Map.Entry entry = (Map.Entry)i$.next();
                    if(entry.getValue() instanceof Iterable) {
                        Iterable value = (Iterable)entry.getValue();
                        e.println((String)entry.getKey() + ":");
                        Iterator i$1 = value.iterator();

                        while(i$1.hasNext()) {
                            Object o = i$1.next();
                            e.println(o);
                        }
                    } else {
                        e.println((String)entry.getKey() + ":\t" + entry.getValue());
                    }
                }

                e.close();
                break;
            }
        } catch (IOException var10) {
            this.logger.warn("write file error", var10);
        }

    }
}

